/**
 * This file is part of Tamariba. Copyright (C) 2011 Michael Vogt <michael@tamariba.eu>, Patrick Hempel
 * <patrick@tamariba.eu>
 * 
 * Tamariba is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 * 
 * Tamariba is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with Tamariba. If not, see
 * <http://www.gnu.org/licenses/>.
 */
package eu.tamariba.html.shared;


/**
 * Value-Class for the duration of a {@link Task} and Step.
 * 
 * Contains all the methods to increase or decrease a timespan. Also creates and parses the String to store and read the
 * duration to the db.
 * 
 * @author michael vogt
 */
public class Duration {
   private static final String kDaySeparator = "D";
   private static final String kMinuteSeparator = ":";
   
   private static final int kMaxMinute = 59;
   private static final int kMaxHour = 23;
   
   private int day;
   private int hour;
   private int minute;
   
   
   public Duration( int day, int hour, int minute) {
      this.day = day;
      this.hour = hour;
      this.minute = minute;
   }
   
   
   public Duration( String durationString) {
      splitDurationString( durationString);
   }
   
   
   /**
    * Splits the String representation of the duration. Expects the String in the format "dDhh:mm".
    * 
    * @param durationString
    */
   private void splitDurationString( String durationString) {
      // Check for the day
      String[] daySplit = durationString.split( kDaySeparator);
      if( 2 > daySplit.length) {
         throw new IllegalArgumentException();
      }
      
      // Split hours and minutes
      String[] hourSplit = daySplit[1].split( kMinuteSeparator);
      if( 2 > hourSplit.length) {
         throw new IllegalArgumentException();
      }
      
      try {
         this.day = Integer.parseInt( daySplit[0]);
         this.hour = Integer.parseInt( hourSplit[0]);
         this.minute = Integer.parseInt( hourSplit[1]);
      } catch( NumberFormatException exception) {
         throw new IllegalArgumentException( exception);
      }
   }
   
   
   public String getDurationString() {
	  String hourString = 10 > hour ? "0" + hour : String.valueOf(hour);
	  String minuteString = 10 > minute ? "0" + minute : String.valueOf(minute);
	   
	  StringBuilder builder = new StringBuilder();
	  builder.append(day)
	  	.append(kDaySeparator)
	  	.append(hourString)
	  	.append(kMinuteSeparator)
	  	.append(minuteString);
	  
      return builder.toString();
   }
   
   
   public void increase() {
      minute++;
      
      if( kMaxMinute < minute) {
         minute = 0;
         hour++;
         
         if( kMaxHour < hour) {
            hour = 0;
            day++;
         }
      }
   }
   
   
   /**
    * Decrease the duration, until day, hour and minute are down to zero.
    * 
    * @return boolean, true when all values are down to zero, false otherwise
    */
   public boolean decrease() {
      boolean result = false;
      
      minute--;
      if( 0 > minute) {
         minute = kMaxMinute;
         hour--;
         
         if( 0 > hour) {
            hour = kMaxHour;
            day--;
            
            if( 0 > day) {
               minute = 0;
               hour = 0;
               day = 0;
               
               result = true;
            }
         }
      } else if( 0 == minute && 0 == hour && 0 == day) {
         result = true;
      }
      
      return result;
   }
}